package security.channel;

import java.io.IOException;
import java.net.Socket;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;

/**
 * This class represents a channel over a network thats messages are encrypted
 * or decrpyted by using any AES algorithm.
 */
public class AESChannel extends SecureChannel {

	//used algorithm for encryption and decryption
	public static final String algorithm = "AES/CTR/NoPadding";

	public AESChannel(Socket socket, SecretKey keyAES, IvParameterSpec ivparam)
			throws IOException {
		super(socket);

		// initialize cipher with specified algorithm
		createCipher(algorithm);

		try { // initialize cipher to encrypt and decrypt
			this.decrypt.init(Cipher.DECRYPT_MODE, keyAES, ivparam);
			this.encrypt.init(Cipher.ENCRYPT_MODE, keyAES, ivparam);
		} catch (InvalidKeyException e) {
			System.err.println("ERROR: init cipher to en- or decrpyt in "
					+ this.getClass() + ", invalidkeyexception: "
					+ e.getMessage());
		} catch (InvalidAlgorithmParameterException e) {
			System.err
					.println("ERROR: init cipher to decrypt in "
							+ this.getClass()
							+ ", InvalidAlgorithmParameterException: "
							+ e.getMessage());
		}
	}
}